﻿/*
using StackExchange.Redis;
using System;
using System.Threading.Tasks;
using Maltose.Configuration;

namespace Maltose.Cache
{

    /// <summary>
    /// Redis 缓存操作
    /// </summary>
    public class RedisManage : ICache
    {
        /// <summary>
        /// 默认读取配置文件中的值 RedisConnectString 值
        /// </summary>
        static string ConnectString = UseConfigFactory.Json.Get<string>("RedisConnectionString") ?? "localhost";

        //锁
        static readonly object _lock = new object();

        static ConnectionMultiplexer _Connection;

        /// <summary>
        /// 连接对象
        /// </summary>
        static ConnectionMultiplexer Connection
        {
            get
            {
                if (_Connection == null || !_Connection.IsConnected) //如果连接为空 or 连接断开
                {
                    lock (_lock)
                    {
                        if (_Connection == null || !_Connection.IsConnected)
                        {
                            _Connection = ConnectionMultiplexer.Connect(ConnectString);
                            return _Connection;
                        }
                    }
                }
                return _Connection;
            }
        }

        /// <summary>
        /// 内部访问数据库
        /// </summary>
        IDatabase RedisClient { get; set; }

        /// <summary>
        /// Redis 缓存操作
        /// </summary>
        public RedisManage()
        {
            RedisClient = Connection.GetDatabase();
        }

        /// <summary>
        ///  Redis 缓存操作
        /// </summary>
        /// <param name="connString">连接字符串</param>
        public RedisManage(string connString)
        {
            ConnectString = connString;
            RedisClient = Connection.GetDatabase();
        }

        /// <summary>
        /// 添加一个缓存值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">内容</param>
        /// <param name="expiry">时间</param>
        public bool Add(string key, object value, TimeSpan? expiry = null)
        {
            return RedisClient.StringSet(key, value.ToString(), expiry);
        }

        /// <summary>
        /// 异步添加一个缓存值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">内容</param>
        /// <param name="expiry">时间</param>
        /// <returns></returns>
        public async Task<bool> AddAsync(string key, object value, TimeSpan? expiry = null)
        {
            return await RedisClient.StringSetAsync(key, value.ToString(), expiry);
        }

        /// <summary>
        /// 插入一个集合值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="entity"></param>
        /// <param name="expiry"></param>
        /// <returns></returns>
        public bool AddList<T>(string key, T entity, TimeSpan? expiry = null) where T : class
        {
            return RedisClient.StringSet(key, entity.ToJson(), expiry);
        }

        /// <summary>
        /// 异步插入一个集合值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="entity"></param>
        /// <param name="expiry"></param>
        /// <returns></returns>
        public async Task<bool> AddListAsync<T>(string key, T entity, TimeSpan? expiry = null) where T : class
        {
            return await RedisClient.StringSetAsync(key, entity.ToJson(), expiry);
        }

        /// <summary>
        /// 移除缓存中的对应键
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public bool Delete(string key)
        {
            return RedisClient.KeyDelete(key);
        }

        /// <summary>
        /// 异步移除缓存中的对应键
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(string key)
        {
            return await RedisClient.KeyDeleteAsync(key);
        }


        /// <summary>
        /// 获取一个缓存值 键不存在 返回 null
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public string Get(string key)
        {
            return RedisClient.StringGet(key);
        }

        /// <summary>
        /// 获取一个缓存值 键不存在 返回 null
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public async Task<string> GetAsync(string key)
        {
            return await RedisClient.StringGetAsync(key);
        }

        /// <summary>
        /// 获取一组泛型值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public T GetList<T>(string key) where T : class, new()
        {
            if (KeyExists(key))
            {
                return RedisClient.StringGet(key).ToString().JsonToObject<T>();
            }
            return default;
        }


        /// <summary>
        /// 异步获取一组泛型值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<T> GetListAsync<T>(string key) where T : class, new()
        {
            if (KeyExists(key))
            {
                return await Task.Run<T>(() =>
                {
                    return RedisClient.StringGetAsync(key).ToString().JsonToObject<T>();
                });
            }
            return await Task.Run(() => { return default(T); });
        }

        /// <summary>
        /// 判断当前键是否存在
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public bool KeyExists(string key)
        {
            return RedisClient.KeyExists(key);
        }

        /// <summary>
        /// 判断当前键是否存在
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public async Task<bool> KeyExistsAsync(string key)
        {
            return await RedisClient.KeyExistsAsync(key);
        }

        /// <summary>
        /// 添加哈希表
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="field">哈希键</param>
        /// <param name="value">保存的值</param>
        /// <returns></returns>
        public bool AddHash(string key, string field, string value)
        {
            return RedisClient.HashSet(key, field, value);
        }

        /// <summary>
        /// 获取哈希表值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="field">哈希键</param>
        /// <returns></returns>
        public object GetHashValue(string key, string field)
        {
            return RedisClient.HashGet(key, field);
        }

        /// <summary>
        /// 获取哈希表转换成 对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="field">哈希键</param>
        /// <returns></returns>
        public T GetHashValue<T>(string key, string field) where T : class, new()
        {
            return GetHashValue(key, field).ToString().JsonToObject<T>();
        }

        /// <summary>
        /// 判断hash key 是否存在
        /// </summary>
        /// <param name="key">哈希表键</param>
        /// <param name="field">哈希表键字段</param>
        /// <returns></returns>
        public bool HashKeyExists(string key, string field)
        {
            return RedisClient.HashExists(key, field);
        }

        /// <summary>
        /// 判断hash key 是否存在
        /// </summary>
        /// <param name="key">哈希表键</param>
        /// <param name="field">哈希表键字段</param>
        /// <returns></returns>
        public async Task<bool> HashKeyExistsAsync(string key, string field)
        {
            return await RedisClient.HashExistsAsync(key, field);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key">删除哈希表键</param>
        /// <param name="field">删除哈希表键字段</param>
        /// <returns></returns>
        public bool DeleteHash(string key, string field)
        {
            return RedisClient.HashDelete(key, field);
        }

        /// <summary>
        /// 删除Async
        /// </summary>
        /// <param name="key">删除哈希表键</param>
        /// <param name="field">删除哈希表键字段</param>
        /// <returns></returns>
        public async Task<bool> DeleteHashAsync(string key, string field)
        {
            return await RedisClient.HashDeleteAsync(key, field);
        }

        /// <summary>
        /// 删除多个key支持正则匹配
        /// </summary>
        /// <param name="patternKeys"></param>
        /// <returns></returns>
        public bool DeleteKeys(string patternKeys)
        {

            var luaScript = LuaScript.Prepare("local res = redis.call('KEYS', @patternKeys) return res ");
            var keys = RedisClient.ScriptEvaluate(luaScript, new { patternKeys });
            if (!keys.IsNull)
            {
                RedisClient.KeyDelete((RedisKey[])keys);
            }
            return true;
        }

        /// <summary>
        /// 删除多个key支持正则匹配
        /// </summary>
        /// <param name="patternKeys"></param>
        /// <returns></returns>
        public async Task<bool> DeleteKeysAsync(string patternKeys)
        {
            var luaScript = LuaScript.Prepare("local res = redis.call('KEYS', @patternKeys) return res ");
            var keys = await RedisClient.ScriptEvaluateAsync(luaScript, new { patternKeys });
            if (!keys.IsNull)
            {
                await RedisClient.KeyDeleteAsync((RedisKey[])keys);
            }
            return await Task.Run(() => { return true; });
        }

        public bool Add(string key, object value, TimeSpan expiry)
        {
            throw new NotImplementedException();
        }

        public bool Add(string key, object value, int expireSeconds = -1)
        {
            throw new NotImplementedException();
        }

        public Task<bool> AddAsync(string key, object value, TimeSpan expiry)
        {
            throw new NotImplementedException();
        }

        public Task<bool> AddAsync(string key, object value, int expireSeconds = -1)
        {
            throw new NotImplementedException();
        }

        public bool AddList<T>(string key, T entity, TimeSpan expiry) where T : class
        {
            throw new NotImplementedException();
        }

        public bool AddList<T>(string key, T entity, int expireSeconds = -1) where T : class
        {
            throw new NotImplementedException();
        }

        public Task<bool> AddListAsync<T>(string key, T entity, TimeSpan expiry) where T : class
        {
            throw new NotImplementedException();
        }

        public Task<bool> AddListAsync<T>(string key, T entity, int expireSeconds = -1) where T : class
        {
            throw new NotImplementedException();
        }

        public bool DeleteKeys(string[] keys)
        {
            throw new NotImplementedException();
        }

        public Task<bool> DeleteKeysAsync(string[] keys)
        {
            throw new NotImplementedException();
        }
    }
}
*/